package com.wealth.scsj.b_app.view.activity;

import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.alibaba.fastjson.JSONObject;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.google.gson.Gson;
import com.orhanobut.logger.Logger;
import com.wealth.scsj.b_app.R;
import com.wealth.scsj.b_app.adapter.LeftAdapter;
import com.wealth.scsj.b_app.adapter.RightAdapter;
import com.wealth.scsj.b_app.base.GoodsManageBase;
import com.wealth.scsj.b_app.bean.LeftBean;
import com.wealth.scsj.b_app.bean.RightBean;
import com.wealth.scsj.b_app.bean.SelectAppClass1Bean;
import com.wealth.scsj.b_app.bean.SelectAppGoodsByStoreAndClassBean;
import com.wealth.scsj.b_app.event.FirstEvent;
import com.wealth.scsj.b_app.presenter.GoodsManagePresenter;
import com.wealth.scsj.b_app.utils.RxBus;
import com.wealth.scsj.b_app.utils.SkipUtils;

import java.util.ArrayList;
import java.util.List;

import es.dmoral.toasty.Toasty;
import okhttp3.RequestBody;
import rx.android.schedulers.AndroidSchedulers;
import rx.functions.Action1;
import rx.schedulers.Schedulers;

/**
 * 商品管理
 */
public class GoodsManageActivity extends BaseActivity implements View.OnClickListener, GoodsManageBase.IView {

    private List<RightBean> rightBeanList;
    private List<LeftBean> leftBeanList;
    private RecyclerView left_rv;
    private RecyclerView right_rv;
    private RightAdapter mAdapter;
    private LeftAdapter leftAdapter;
    private TextView mToolbarTitle;
    private TextView mToolbarTitleLeft;
    /**
     * 编辑
     */
    private TextView mToolbarTitleRight;
    private ImageView mToolbarTitleRifhtImg;
    private GoodsManagePresenter presenter;
    private List<SelectAppClass1Bean.DataBean> leftData;
    private List<SelectAppGoodsByStoreAndClassBean.DataBean> rightData;

    @Override
    protected void createView(Bundle savedInstanceState) {
        setContentView(R.layout.activity_goods_manage);

        presenter = new GoodsManagePresenter(this);

        JSONObject jsonObject = new JSONObject();
        // TODO: 2018/7/31 店家ID暂时写死
        jsonObject.put("id","c2f1a897-7b67-11e8-afb7-70106fb15682");
        RequestBody body = RequestBody.create(okhttp3.MediaType.parse("application/json;charset=utf-8"), jsonObject.toString());
        presenter.deliver1Data(body);
    }

    @Override
    protected void initView() {
        left_rv = findViewById(R.id.left_rv);
        left_rv.setLayoutManager(new LinearLayoutManager(this));

        right_rv = findViewById(R.id.right_rv);
        right_rv.setLayoutManager(new LinearLayoutManager(this));
        mToolbarTitle = findViewById(R.id.toolbar_title);
        mToolbarTitleLeft =  findViewById(R.id.toolbar_title_left);
        mToolbarTitleRight =  findViewById(R.id.toolbar_title_right);
        mToolbarTitleRifhtImg = findViewById(R.id.toolbar_title_rifht_img);
    }

    @Override
    protected void initListener() {
        mToolbarTitleLeft.setOnClickListener(this);
        mToolbarTitleRight.setOnClickListener(this);

        RxBus.getDefault().toObservable(FirstEvent.class)
                //在io线程进行订阅，可以执行一些耗时操作
                .subscribeOn(Schedulers.io())
                //在主线程进行观察，可做UI更新操作
                .observeOn(AndroidSchedulers.mainThread())
                //观察的对象
                .subscribe(new Action1<FirstEvent>() {
                    @Override
                    public void call(FirstEvent user) {
                        if (user.getCode().equals("LeftAdapter"))
                            exampleData(user.getTitle());
                    }
                });

    }

    private void exampleData(String title) {
        JSONObject jsonObject = new JSONObject();
        // TODO: 2018/7/31 店家ID暂时写死
        jsonObject.put("storeId","c2f1a897-7b67-11e8-afb7-70106fb15682");
        jsonObject.put("goodsClassId",title);
        RequestBody body = RequestBody.create(okhttp3.MediaType.parse("application/json;charset=utf-8"), jsonObject.toString());
        presenter.deliver2Data(body);
    }

    @Override
    protected void initData() {
        mToolbarTitle.setText("商品管理");
        mToolbarTitleRight.setText("+新增商品");
        mToolbarTitleRight.setVisibility(View.VISIBLE);
        rightBeanList = new ArrayList<>();
        for (int i = 0; i < 6; i++) {
            rightBeanList.add(new RightBean("", "fff", "fff", "fff", "fff", "fff"));
        }

//        mAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
//            @Override
//            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
////                Toasty.info(GoodsManageActivity.this, "点击了" + position).show();
//
//                view.findViewById(R.id.editing_product_tv).setOnClickListener(new View.OnClickListener() {
//                    @Override
//                    public void onClick(View v) {
//                        SkipUtils.toEditingClassificationPage(GoodsManageActivity.this);
//                    }
//                });
//            }
//        });

        leftBeanList = new ArrayList<>();
        for (int i = 0; i < 6; i++) {
            leftBeanList.add(new LeftBean("fff", "fff"));
        }

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            default:
                break;
            case R.id.toolbar_title_left:
                finish();
                break;
            case R.id.toolbar_title_right:
                SkipUtils.toAddCommodityPage(GoodsManageActivity.this);
                break;
        }
    }

    @Override
    public void show1SuccessMsg(String s, SelectAppClass1Bean selectAppClass1Bean) {
        String s1 = new Gson().toJson(selectAppClass1Bean);
        Logger.e("分类1==》"+s1);
        leftData = selectAppClass1Bean.getData();
        leftAdapter = new LeftAdapter(leftData);
        left_rv.setAdapter(leftAdapter);
    }

    @Override
    public void show2SuccessMsg(String s, SelectAppGoodsByStoreAndClassBean selectAppGoodsByStoreAndClassBean) {
        String s1 = new Gson().toJson(selectAppGoodsByStoreAndClassBean);
        Logger.e("分类2==》"+s1);
        rightData = selectAppGoodsByStoreAndClassBean.getData();
        mAdapter = new RightAdapter(this, rightData);
        right_rv.setAdapter(mAdapter);
    }

    @Override
    public void showErrorMsg(String s) {

    }
}
